<?php
namespace App\Repositories;

use App\Models\GameRoom;
use App\Http\Controllers\Uploads;
use Illuminate\Support\Facades\Redis;
use App\Models\GameServer\OnlineUser;

/**
*
*/
class GameRoomRepository
{
    protected $redis;

    public function __construct()
    {
        $this->redis = Redis::connection();
    }

    //获取所有房间
    public function getAllRooms()
    {
        $rooms = GameRoom::all();

        foreach ($rooms as $key => &$room) {
            //在线玩家统计
            $gamer_count = $this->redis->hget('room_gamer_counts', 'room:' . $room->room_id);
            $room->gamer_count = !empty($gamer_count) ? $gamer_count : 0;
        }

        return $rooms;
    }

    //添加房间
    public function addRoom($request)
    {
        // pd($request->all());
        $data = explode(',', $request->jeton);

        // $jeton = [];
        // for ($i=0; $i < count($data); $i++) {
        //     $key = 'jeton_' . ($i+1);
        //     $jeton[$key] = $data[$i];
        // }
        // extract($jeton);
        list($jeton_1, $jeton_2, $jeton_3, $jeton_4, $jeton_5) = $data;

        $room = GameRoom::create([
            'room_name'       => $request->room_name,
            // 'room_type'       => $request->room_type,
            'room_type'       => 0,
            'max'             => (int)$request->max,
            'min'             => (int)$request->min,
            'table_count'     => 4,
            'thumbnail'       => $request->thumbnail,
            'status'          => (int)$request->status,
            'sort_id'         => (int)$request->sort_id,
            'enter_limit_max' => (int)$request->enter_limit_max,
            'experience_base' => (int)$request->experience_base,
            'experience_rate' => (float)$request->experience_rate,
            'jeton_1'         => isset($jeton_1) ? (int)$jeton_1 : 0,
            'jeton_2'         => isset($jeton_2) ? (int)$jeton_2 : 0,
            'jeton_3'         => isset($jeton_3) ? (int)$jeton_3 : 0,
            'jeton_4'         => isset($jeton_4) ? (int)$jeton_4 : 0,
            'jeton_5'         => isset($jeton_5) ? (int)$jeton_5 : 0,
        ]);

        if ($room && $room->status == 1) {
            // $redis = Redis::connection();
            $this->redis->hset('room_lists', 'room:' . $room->room_id, json_encode($room));
        }


        return $room;
    }

    //暂时没用
    public function thumbnail($request)
    {
        if ($request->hasFile('thumbnail')) {
            $file = $request->thumbnail;
            $name = str_random(10) . 'jpg';
            $filePath = public_path() . '/uploads/thumbnails/' . $name;
            $width = 100;
            $height = 100;
            $path = Uploads::images($file, $filePath, $width, $height);

            return $path;
        }
    }

    //获取房间
    public function getRoom($id)
    {
        return GameRoom::find($id);
    }

    //更新房间
    public function updateRoom($request, $id)
    {
        // pd($request->all());
        $room = GameRoom::find($id);

        $data = explode(',', $request->jeton);
        list($jeton_1, $jeton_2, $jeton_3, $jeton_4, $jeton_5) = $data;

        $room->room_name       = $request->room_name;
        // $room->room_type       = $request->room_type;
        $room->max             = (int)$request->max;
        $room->min             = (int)$request->min;
        $room->enter_limit_max = (int)$request->enter_limit_max;
        $room->experience_rate = (float)$request->experience_rate;
        $room->experience_base = (int)$request->experience_base;
        $room->status          = (int)$request->status;
        $room->sort_id          = (int)$request->sort_id;
        $room->thumbnail       = $request->thumbnail;
        $room->jeton_1         = isset($jeton_1) ? (int)$jeton_1 : 0;
        $room->jeton_2         = isset($jeton_2) ? (int)$jeton_2 : 0;
        $room->jeton_3         = isset($jeton_3) ? (int)$jeton_3 : 0;
        $room->jeton_4         = isset($jeton_4) ? (int)$jeton_4 : 0;
        $room->jeton_5         = isset($jeton_5) ? (int)$jeton_5 : 0;

        $room->save();

        if ($room && $room->status == 1) {
            // $redis = Redis::connection();
            $this->redis->hset('room_lists', 'room:' . $id, json_encode($room));
        }

        return true;
    }

    //删除
    public function deleteRoom($id)
    {
        if (GameRoom::destroy($id)) {
            $this->redis->hdel('room_lists', 'room:' . $id);

            return true;
        }

        return false;

    }

    //批量删除
    public function deleteRoomsLists($ids)
    {
        if (GameRoom::whereIn('room_id', $ids)->delete()) {
            $rooms = $this->redis->hkeys('room_lists');

            for ($i=0; $i < count($ids); $i++) {
                $id = $ids[$i];
                if (in_array('room:'. $id, $rooms)) {
                    $this->redis->hdel('room_lists', 'room:' . $id);
                }
            }
        }
        return true;
    }

    public function editStatus($id, $status)
    {
        $room = GameRoom::find($id);
        $room->status = $status;
        $room->save();

        $string = $this->redis->hget('room_lists', 'room:'. $id);

        $room = json_decode($string);
        $room->status = $status;

        $this->redis->hset('room_lists', 'room:'. $id, json_encode($room));
        return true;
    }

}